﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DaInterFace;
using DaInterFace.SYN;
using DaInterFace.SYN.E01;

namespace E01
{
    /// <summary>
    /// 同步大宗原料
    /// </summary>
    public class A32 : ISYNChildSet
    {
        public void Code(ISYN handler, CodeResult result, SYNRight user, SerialType st, String parameter)
        {
            E01A32P P = (E01A32P)handler.Serial.Deserialize(st, typeof(E01A32P), parameter);

            try
            {
                SqlParameter[] sps = new SqlParameter[4];
                sps[0] = new SqlParameter("@ID", SqlDbType.NVarChar, 50);
                sps[0].Value = P.ID;
                sps[1] = new SqlParameter("@Number", SqlDbType.NVarChar, 50);
                sps[1].Value = P.Number;
                sps[2] = new SqlParameter("@Name", SqlDbType.NVarChar, 50);
                sps[2].Value = P.Name;
                //sps[3] = new SqlParameter("@CalPlan", SqlDbType.NVarChar, 50);
                //sps[3].Value = P.CalPlan;
                //sps[3] = new SqlParameter("@StockNum", SqlDbType.Decimal);
                //sps[3].Value = P.StockNum;
                sps[3] = new SqlParameter("@materialID", SqlDbType.NVarChar, 50);
                sps[3].Value = P.materialID;
               

                StringBuilder sql = new StringBuilder();
                sql.Append("SET NOCOUNT ON\r\n");;
                //sql.Append("DECLARE @CalPlanFID INT\r\n SET @CalPlanFID = IsNull((SELECT FID FROM KEW_PurchConfigEntry WHERE FText =@CalPlan AND @CalPlan<>''),0)\r\n");
                sql.Append("DECLARE @FID INT\r\n SET @FID=IsNull((SELECT FID FROM KEW_PurchItemEntry WHERE FText = @ID),0) \r\n");
                sql.Append("Declare @material int select @material = isnull(FItemID,0) from t_ICItem where F_102 = @materialID \r\n");
                sql.Append("IF( @FID is not null and @FID>0) BEGIN\r\n");
                sql.Append("  UPDATE KEW_PurchItem SET FName=@Name,FNumber=@Number WHERE FID = @FID \r\n");
                sql.Append("  UPDATE KEW_PurchItemEntry SET FName=@Name,FBase1=0,FBase = @material,FNumber=@Number WHERE FID = @FID \r\n");
                sql.Append("  SELECT 'U',@FID\r\n");
                sql.Append("END \r\nELSE BEGIN\r\n");
                sql.Append("  EXEC GetICMaxNum 'KEW_PurchItem',@FID OUTPUT\r\n");
                sql.Append("  INSERT INTO KEW_PurchItem ([FID],[FNumber],[FName],[FParentID],[FLogic],[FDetail],[FDiscontinued],[FLevels],[FFullNumber],[FClassTypeID]) VALUES (@FID,@Number,@Name,0,-1,1,0,1,@Number,236900126)\r\n");
                sql.Append("  INSERT INTO KEW_PurchItemEntry ([FClassTypeID],[FID],[FIndex],[FNumber],[FName],[FBase5],[FBase1],[FBase4],[FBase2],[FBase3],[FDecimal],[FText],[FBase]) ");
                sql.Append("  VALUES (236900126,@FID,0,@Number,@Name,0,0,0,0,0,0,@ID,@material)\r\n");
                sql.Append("  SELECT 'I',@FID\r\n");
                sql.Append("END \r\n");
                sql.Append("SET NOCOUNT OFF\r\n");
                Recordset rs = handler.Data.ExeProcedure(P.BaseNo, sql.ToString(), sps);
                if (rs.Enable)
                {
                    if (rs.Data.Tables[0].Rows[0][0].ToString() == "U")
                    {
                        result.Result = EnumResult.Update;
                        result.Content = rs.Data.Tables[0].Rows[0][1].ToString();
                    }
                    else
                    {
                        result.Result = EnumResult.Insert;
                        result.Content = rs.Data.Tables[0].Rows[0][1].ToString();
                    }
                    result.Message = String.Format("大宗原料同步完成:{0},{1}", P.Number, result.Result.ToString());
                }
                else
                {
                    result.Result = EnumResult.None;
                    result.Message = String.Format("大宗原料同步失败。{0}", "可能原因是:引用的基础资料未同步、信息录入错误或网络故障！");
                }
            }
            catch (Exception ex)
            {
                result.Result = EnumResult.None;
                result.Message = String.Format("大宗原料同步失败:{0}", ex.Message);
            }
        }

    }

    /// <summary>
    /// 大宗原料
    /// </summary>
    public class E01A32P
    {
        /// <summary>
        /// 账套编号
        /// </summary>
        public String BaseNo;
        /// <summary>
        /// 三方内码
        /// </summary>
        public String ID;
        /// <summary>
        /// 三方编号
        /// </summary>
        public String Number;
        /// <summary>
        /// 三方名称
        /// </summary>
        public String Name;
        /// <summary>
        /// 计量方案
        /// </summary>
        public String CalPlan;
        /// <summary>
        /// 库存数量
        /// </summary>
        public Decimal StockNum;
        /// <summary>
        /// 物料
        /// </summary>
        public string materialID;

    }
}
